const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    list: [],
    showNoData: false,
    page: 1,
    loadingStatus: 0,   //上拉加载状态 0停止 1开启 2没有更多数据
    loadingVal: [0, "加载更多中...", "没有更多啦~"],
  },
  onLoad(){
    this.getBalanceUseRecorde();
  },
  
  /*分享*/
  onShareAppMessage(){} ,
  
  /*监听用户下拉动作*/
  onPullDownRefresh(){
    let _this = this, td = this.data;
    SERVICE.POST(SERVICE.URL.balanceUseRecorde, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      page: td.page
    }, (status, res) => {
      wx.stopPullDownRefresh();    //停止下拉    
      if (res.data.status === "success") {
        let list = res.data.rentalList;
        console.log(list);
        if (list !== "nothing") {
          _this.setData({
            list: td.list.concat(list)
          });
        }
      } else {
        _this.setData({
          showNoData: true
        });
      }
    }); 
  },

  /*页面上拉触底事件的处理函数*/
  onReachBottom(){
    let _this = this, td = this.data;
    if (td.loadingStatus === 0){
      _this.setData({
        loadingStatus: 1
      });
      SERVICE.POST(SERVICE.URL.balanceUseRecorde, {
        thirdCookie: wx.getStorageSync("thirdCookie"),
        page: td.page += 1
      }, (status, res) => {
        if (res.data.status === "success") {
          let list = res.data.rentalList;
          if (list !== "nothing") {
            _this.setData({
              loadingStatus: 0,
              list: td.list.concat(list)
            });
          } else {
            _this.setData({
              loadingStatus: 2
            });
          }
        }
      });
    }
  },

  /**
   * @explain balanceUseRecorde 余额使用记录
   * @param thirdCookie 用户标识
   * @param page 页数
   * 
   * */ 
  getBalanceUseRecorde(){
    wx.showNavigationBarLoading();
    let _this = this, td = this.data;
    SERVICE.POST(SERVICE.URL.balanceUseRecorde,{
      thirdCookie: wx.getStorageSync("thirdCookie"),
      page: td.page
    },(status,res)=>{
      console.log("余额使用记录:",res);
      wx.hideNavigationBarLoading();
      if(res.data.status === "success"){
        let list = res.data.rentalList;
        if (list !== "nothing"){
          _this.setData({
            list: td.list.concat(list)
          });
        }else{
          _this.setData({
            showNoData: true
          });
        }
      }
    });
  }
});